//入口函数 
$(function(){
	//传入pageIndex=1
	findOvertime(1);
	
//	//查询审批人
//	showApprovalPersons();
	
	//首页
	$("#firstPage").click(function(){
//		alert("666");
		var currentpage = parseInt($("#pageIndex").html());
		if(currentpage!=1){
			findOvertime(1);
		}
	});
	//下一页操作
	$("#nextPage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		var totalpage = parseInt($("#totalPage").html());
		if(currentpage < totalpage){
			findOvertime(currentpage+1);
		}
	});
	//上一页操作
	$("#prePage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		if(currentpage > 1){
			findOvertime(currentpage-1);
		}
	});
	//尾页
	$("#endPage").click(function(){
		var currentpage = parseInt($("#pageIndex").html());
		var totalpage = parseInt($("#totalPage").html());
		if(currentpage != totalpage){
			findOvertime(totalpage);
		}
	});

})

//分页查询
function findOvertime(page){
	//获取页面传入的username
	var name = $("#realName").val();
//	alert(name);
	//条件判断
	$.ajax({
		url: "/Overtime/showOvertimeByPage",
		type: "post",
		data:{
			pageIndex: page,
			username: name
		},
		dataType: "json",
		success: function(data) {
//			alert("请求分页查询成功");
			var content = "";
			var overtimes = data.beanList;
			var totalRecord = data.totalRecord;
			var pageIndex = data.pageIndex;
			var totalPage = data.totalPage;
			
			content+='<table class="table table-bordered" style="margin-top: 20px;">'
	               +'<thead>'
	               +'<tr>'
	               +'<th style="display:none">ID</th>'
	               +'<th>姓名</th>'
	               +'<th>申请时间</th>'
	               +'<th>开始时间</th>'
	               +'<th>结束时间</th>'
	               +'<th>加班时长</th>'
	               +'<th>描述</th>'
	               +'<th>审批状态</th>'
	               +'<th style="width: 150px;">操作</th>'
	               +'</tr>'
	               +'</thead>';
			for(var i = 0;i<overtimes.length;i++){
				var po = overtimes[i];
//				alert(po.om_username);
				//将po对象转为json字符串
				var jpo = JSON.stringify(po);
				content+='<tbody>'
					+'<tr class="success">'
					+'<td style="display:none"></td>'
					+'<td>'+po.om_username+'</td>'
					+'<td>'+po.om_applicationtime+'</td>'
					+'<td>'+po.om_starttime+'</td>'
					+'<td>'+po.om_endtime+'</td>'
					+'<td>'+po.om_overtimehours+'</td>'
					+'<td>'+po.om_description+'</td>'
					+'<td>'+po.om_approvalstatus+'</td>'
					+'<td>'
					+"<input type='button' class='btn btn-primary btn' value='修改' onclick='showOvertime("+jpo+")'/>"
					+'<input type="button" class="btn btn-primary btn" value="删除" onclick="delOvertime('+po.om_id+')"/>'
					+'</td>'
					+'</tr>'
					+'</tbody>';
			}
			content+='</table>'
	
		$("#overtimeInfo").html(content);
		$("#totalRecord").html(totalRecord);
		$("#pageIndex").html(page);
		$("#totalPage").html(totalPage);
			
		},error: function(data) {
			alert("系统错误，请联系管理员");
		}
	});
	
}


/**
 * 添加加班
 * @param path
 */
function addOvertime() {
	var startTime = $("#startTime").val();
//	alert(startTime);
	var endTime = $("#endTime").val();
//	alert(endTime);
	var des = $("#des").val();
	var res =  validate();
	if (!res) return;
	$.ajax({
		url: "/Overtime/addOvertimeInfo",
		type: "POST",
		data:{
			beginTime:startTime,
			endTime:endTime,
			des:des
		},
		dataType: "text",
		success: function(data) {
//			alert("请求添加成功，请继续");
			if (data == 1) {
				alert("添加成功");
				$('#overtimeDivModel').remove();
				window.location.href="/overtime.html";
			} else if (data == 0) {
				alert("添加失败");
			} else if(data == 2){
				alert("添加失败，所选项不能为空");
			}else {
				alert(data);
			}
		},error: function(data) {
			alert("系统错误，请联系管理员");
		}
	});
}

/**
 * 修改加班
 * @param path
 */
function updOvertime() {
	var id = $("#md_overtimeId").val();
//	alert(id);
	var startTime = $("#md_startTime").val();
//	alert(startTime);
	var endTime = $("#md_endTime").val();
	var des = $("#md_des").val()
	var res =  md_validate();
	if (!res) return; 
	$.ajax({
		url: "/Overtime/modifyOvertime",
		type: "post",
		data:{
			id:id,
			startTime:startTime,
			endTime:endTime,
			des:des
			},
		dataType: "JSON",
		success: function(data) {
//			alert("修改请求发送成功");
			if(data==1){
//				alert("修改成功");
				$('#overtimeDivModel').remove();
				//刷新页面
				window.location.href="/overtime.html";
			}else if(data==0){
				alert("修改失败");
			}
		}, error: function(data) {
			alert("系统错误，请联系管理员");
		}
	});
}

/**
 * 删除加班
 * @param path
 */
function delOvertime(id) {
	
	if (confirm("确定删除吗")) {
		$.ajax({
			url: "/Overtime/deleteOvertime",
			type: "POST",
			data:{
				id: id
			},
			dataType: "JSON",
			success: function(data) {
//				alert("删除请求发送成功");
				if(data==1){
					alert("删除成功");
					//刷新页面
					findOvertime(1);
				}else if(data==0){
					alert("删除失败");
				}
				
			}, error: function(data) {
				alert("系统错误，请联系管理员");
			}
		});
	}
}

/**
 * 验证添加日期格式
 */
function validate(){
	
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var st = /^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01]) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	if (startTime != '' && !st.test(startTime)){
		alert("请输入正确的开始日期格式");
		return false;
	}
	var et = /^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01]) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	if (endTime != '' && !et.test(endTime)){
		alert("请输入正确的结束日期格式");
		return false;
	}
	var d1 = Date.parse(startTime);
	var d2 = Date.parse(endTime);
	var date = d2 - d1;
	var time = 60*60*1000;
	if(date<0||date<time){
		alert("开始时间应小于结束时间并且间隔大于一小时,请检查!");
		return false;
	}
	return true;
}

/**
 * 验证修改日期格式
 */
function md_validate(){
	
	var startTime = $("#md_startTime").val();
	var endTime = $("#md_endTime").val();
	var st = /^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01]) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	if (startTime != '' && !st.test(startTime)){
		alert("请输入正确的开始日期格式");
		return false;
	}
	var et = /^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01]) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	if (endTime != '' && !et.test(endTime)){
		alert("请输入正确的结束日期格式");
		return false;
	}
	var d1 = Date.parse(startTime);
	var d2 = Date.parse(endTime);
	var date = d2 - d1;
	var time = 60*60*1000;
	if(date<0||date<time){
		alert("开始时间应小于结束时间并且间隔大于一小时,请检查!");
		return false;
	}
	return true;
}
	
/**
 * 添加加班HTML
 * @param path
 */
function overtimeHTML() {
	// 检查当前页面是否弹层，如果有则删除
	if($('#overtimeDivModel') != null && $('#overtimeDivModel') != undefined)
		$('#overtimeDivModel').remove();
	
	var style_ = 'width: 600px; height: 300px; z-index: 1000; position: fixed; top: 50%;margin-top:-150px; left: 50%;margin-left: -300px;background: #fff;border: 1px solid rgba(39, 38, 38, 0.31);border-radius: 10px;padding: 15px;';
	var xiala = 'border: 1px solid black;outline: none;border-radius: 2px;';
	
	var overtimeHtml = '<div id="overtimeDivModel" style="' + style_ + '">';
	overtimeHtml += '<div class="form-horizontal" role="form">';
	overtimeHtml += '<input type="hidden" id="overtimeId" value=""/>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="startTime" class="col-sm-2 control-label">开始时间</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="startTime" placeholder="如：2017-04-19 08:00:00">';
	overtimeHtml += '</div></div>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="endTime" class="col-sm-2 control-label">结束时间</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="endTime" placeholder="如：2017-04-19 18:00:00">';
	overtimeHtml += '</div></div>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="des" class="col-sm-2 control-label">加班描述</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="des">';
	overtimeHtml += '</div></div>';
//	
//	overtimeHtml += '<div class="form-group">';
//	overtimeHtml += '<label for="des" class="col-sm-2 control-label">审    批    人</label>';
//	overtimeHtml += '<div class="col-sm-10">';
//	overtimeHtml += '<select name="a_eid" id="a_eid" style="' + xiala + '" onclick="showApprovalPersons()">';
//	overtimeHtml += '<option value="">请选择</option>';
//	overtimeHtml += '<option value="">王五</option>';
//	overtimeHtml += '</select>';
//	overtimeHtml += '</div></div>';
	
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<div class="col-sm-12" style="text-align: center;">';

	overtimeHtml += '<input type="button" class="btn btn-primary btn" value="添加" onclick="addOvertime()"/>';
	
	overtimeHtml += '<input type="button" class="btn btn-primary btn" value="取消" onclick="javascript:$(\'#overtimeDivModel\').remove();" style="margin-left: 30px;"/>';
	overtimeHtml += '</div></div>';
	overtimeHtml += '</div></div>';
	$("body").append(overtimeHtml);
	
	//添加日期时间选项框
	lay('#version').html('-v'+ laydate.v);

	/* 添加按钮 */
	//开始时间
	laydate.render({
		elem: '#startTime' ,//指定元素，
		type: 'datetime',	//指定显示格式，既显示日期又显示时间
		format: 'yyyy-MM-dd HH:mm:ss'
	});
	//结束时间
	laydate.render({
	  elem: '#endTime' ,//指定元素
	  type: 'datetime',
	  format: 'yyyy-MM-dd HH:mm:ss'
	});
	
	/**
	 * 查询审批人
	 */
	var personIsOk = true;
	function showApprovalPersons() {
		/* if (personIsOk) { */
//		alert("正在执行查询审批人的方法");
		$.ajax({
			
			url : "/findApprovalPersons",
			type: "post",
			success : function(data) {
//				alert("请求查询审批人成功");
				
				var obj = $("#a_eid");
				/* obj.empty(); */
				for (var i = 0; i < data.length; i++) {
//					alert(data[i].id);
//					alert(data[i].name);
					obj.append('<option value="'+data[i].id+'">' + data[i].name+ '</option>');
				}
				personIsOk = false;
			},
			error : function(data) {
				alert(error);
			}
		});
	}
	
}

//修改加班信息html
function showOvertime(po) {
	// 检查当前页面是否弹层，如果有则删除
	if($('#overtimeDivModel') != null && $('#overtimeDivModel') != undefined)
		$('#overtimeDivModel').remove();
	//通过传入的形参获取当前点击行的加班参数（此处po作为形参，真正的参数是jpo）
	var id = po.om_id;
//	alert(id);
	var name = po.om_username;
	var startTime = po.om_starttime;
//	alert(startTime);
	var endTime = po.om_endtime;
//	alert(endTime);
	var des = po.om_description;
//	alert(des);
	
	var style_ = 'width: 600px; height: 260px; z-index: 1000; position: fixed; top: 50%;margin-top:-150px; left: 50%;margin-left: -300px;background: #fff;border: 1px solid rgba(39, 38, 38, 0.31);border-radius: 10px;padding: 15px;';
	var overtimeHtml = '<div id="overtimeDivModel" style="' + style_ + '">';
	overtimeHtml += '<div class="form-horizontal" role="form">';
	overtimeHtml += '<input type="hidden" id="md_overtimeId" value=""/>';
	
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="startTime" class="control-label"style="margin-left:30px;">用户名：'+name+'</label>';
	overtimeHtml += '</div>';
	
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="startTime" class="col-sm-2 control-label">开始时间</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="md_startTime" placeholder="如：2017-04-19 08:00:00">';
	overtimeHtml += '</div></div>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="endTime" class="col-sm-2 control-label">结束时间</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="md_endTime" placeholder="如：2017-04-19 18:00:00">';
	overtimeHtml += '</div></div>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<label for="des" class="col-sm-2 control-label">加班描述</label>';
	overtimeHtml += '<div class="col-sm-10">';
	overtimeHtml += '<input type="text" class="form-control" id="md_des">';
	overtimeHtml += '</div></div>';
	overtimeHtml += '<div class="form-group">';
	overtimeHtml += '<div class="col-sm-12" style="text-align: center;">';

	overtimeHtml += '<input type="button" class="btn btn-primary btn" value="修改" onclick="updOvertime()"/>';
	
	overtimeHtml += '<input type="button" class="btn btn-primary btn" value="取消" onclick="javascript:$(\'#overtimeDivModel\').remove();" style="margin-left: 30px;"/>';
	overtimeHtml += '</div></div>';
	overtimeHtml += '</div></div>';
	//将拼接的页面显示在页面上
	$("body").append(overtimeHtml);
	//将当前点击行的数据传给修改弹出框中相应的input框，显示当前的加班参数
	$("#md_overtimeId").val(id);
	$("#md_startTime").val(startTime);
	$("#md_endTime").val(endTime);
	$("#md_des").val(des);
	
	//添加日期时间选项框
	lay('#version').html('-v'+ laydate.v);

	/* 添加按钮 */
	//开始时间
	laydate.render({
		elem: '#md_startTime' ,//指定元素，
		type: 'datetime',	//指定显示格式，既显示日期又显示时间
		format: 'yyyy-MM-dd HH:mm:ss'
	});
	//结束时间
	laydate.render({
	  elem: '#md_endTime' ,//指定元素
	  type: 'datetime',
	  format: 'yyyy-MM-dd HH:mm:ss'
	});
	
}
